<!DOCTYPE html>
<html>
    <?php

    include_once "../DB/db.php";
    include_once '../Functions/mySiteFunctions.php';
    include_once '../class/CategoryClass.php';
    include_once '../class/ItemClass.php';
    include_once '../class/UserClass.php';

/*----------------------Funksjoner-----------------------*/
    /*----------------Endre kategori-------------*/
    function siteAlterCat() {   // Displays a form to alter category:
        global $cat;
        $allCategorys = $cat->getAllCategorys();
        ?>
        <h2>Endre kategori</h2>
        <p style="text-decoration: underline; font-size: 19px;">Velg den kategorien du vil endre på:</p>
        <form class="selectCategoryForm" name="selectCategoy" method="POST" action="adminCategory.php">
            <?php radioAllCategorys($allCategorys); ?>
            <input type="submit" id="selectCatButton" name="selectAlterCatSubmit" value="Gå videre" style="width: 100px; font-size: 14px;"/>
        </form>
    <?php
    }
    /*---------------Slette kategori--------------*/
    function siteDeleteCat() { // Displays a form to delete category:
        global $cat;
        $allCategorys = $cat->getAllLivingCategorys();
        ?>
        <h2>Slett kategori</h2>
        <p style="text-decoration: underline; font-size: 19px;">Velg den kategorien du vil slette:</p>
        <form class="selectCategoryForm" name="selectCategoy" method="POST" action="adminCategory.php">
        <?php radioAllCategorys($allCategorys); ?>
            <input type="submit" id="selectCatButton" name="selectDeleteCatSubmit" value="Gå videre" style="width: 100px; font-size: 14px;"/>
        </form>
    <?php
    }
    /*-----------------Ny vare----------------------*/
    function siteNewItem(){ // Displays a form to add new item to category:
        global $cat;
        $allCategorys = $cat->getAllLivingCategorys();
        ?>
        <h2>Slett kategori</h2>
        <p style="text-decoration: underline; font-size: 19px;">Velg kategorien som varen skal ligge under:</p>
        <form class="selectCategoryForm" name="selectCategoy" method="POST" action="adminItem.php">
        <?php radioAllCategorys($allCategorys); ?>
            <input type="submit" id="selectCatButton" name="selectNewItemSubmit" value="Gå videre" style="width: 100px; font-size: 14px;"/>
        </form>
    <?php
    }
    /*--------Hjelper til med displaye kategorier, som radio buttons---------*/
    function radioAllCategorys($allCategorys) {
        while ($row = $allCategorys->fetch()) { // Get all categorys
            $catName = $row['name']; 
            $catId = $row['kid'];
            echo '<input required="required" type="radio" id="catRadio" name="catId" value=' . $catId . ' style="width: 20px;"/>' . $catName . "<br/>";
        }
    }

/*-------------------Selve siden------------------------*/
    if ($user->isLoggedOn() && $user->isAdmin()) {
        ?>
    
        <head>
            <title>Nettbutikk</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link rel="stylesheet" type="text/css" href="../CSS/mainFrame.css" />
            <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
        </head>
        <body>
            <div class="wrapper">
                <?php
                include_once "../MainFrame/header.php";
                include_once "../MainFrame/menuBar.php";
                include_once "../MainFrame/sideBar.php";
                ?>

            <div class="content">
                <h1>Administrator siden</h1>
                <div id="editProfile">
                    <!----Redirect the info to the proper place:---->
                    <?php
                    if (isset($_GET['alterCat']) && $_GET['alterCat'] == "alterCat") { //Endre kategori
                        siteAlterCat();
                    } elseif (isset($_GET['alterCat']) && $_GET['alterCat'] == "deleteCat") { //Slette kategori
                        siteDeleteCat();
                    } elseif (isset($_GET['alterItem']) && $_GET['alterItem'] == "newItem"){ //Ny vare
                        siteNewItem();
                    }
                    ?>
                </div><br>

                <!---------------Lister opp alle valg til administrator ----------->
                <dl id="myProfileList">
                    <dt><h3>Kategorier</h3></dt>
                    <dd><a href="adminCategory.php?alterCat=newCat" class="profileList">Legg til kategori</a></dd>
                    <dd><a href="myAdminSite.php?alterCat=alterCat" class="profileList">Endre kategori</a></dd>
                    <dd><a href="myAdminSite.php?alterCat=deleteCat" class="profileList">Slett kategori</a></dd

                    <dt><h3>Varer</h3></dt>
                    <dd><a href="myAdminSite.php?alterItem=newItem" class="profileList">Legg til vare</a></dd>
                </dl>
            </div>

            <?php
            include_once "../MainFrame/footer.php";
            ?>
        </div>
    </body>


    
    <?php }
    ?>
</html>